<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use JPush\Client as JPush;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function test(){
        /*$client = new JPush("49a057407f2e5590104e6e01", "88a7d48da19c80b0c185c716");
        $client->push()
            ->setPlatform('all')
            ->addAllAudience()
            ->setNotificationAlert('Hello, JPush')
            ->send();*/
        echo 'success';
    }

    public function index(){
        $is_mobile=$this->is_mobile();
        $articles=Article::all();
        if($is_mobile){
            return view('mobile/index')->with('articles',$articles);
        }else{
            return view('index/index')->with('articles',$articles);
        }
    }

    public function articleDetail(){
        $id=$_GET['articleId'];
        $article=Article::find($id);
        $article->browse=$article->browse+1;
        $article->save();
        /*$content=$article->content;
        $txt=substr($content,0,100).'...';*/
        $is_mobile=$this->is_mobile();
        if($is_mobile){
            return view('mobile/articleDetail')->with('article',$article);
        }else{
            return view('index/articleDetail')->with('article',$article);
        }
    }

    //手机网页跳转
    //如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true
    public function is_mobile(){
            $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
            $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
            $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
            $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
            $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
            $regex_match.=")/i";
            return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
    }
}
